package com.example.imtoken.modules.main.wallet.details.trade.transfer

import androidx.lifecycle.MutableLiveData
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.http.BaseObserver
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.core.utils.Utils
import com.example.imtoken.entitys.app.TransferInfo
import com.example.imtoken.entitys.http.GasEntity
import com.example.imtoken.entitys.http.ResponseEntity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class TransferViewModel : BaseViewModel() {

    private val gasEntity = MutableLiveData<GasEntity>()
    fun getGasEntity(): MutableLiveData<GasEntity> {
        return gasEntity
    }

    private val transferInfo = MutableLiveData<TransferInfo>()
    fun getTransferInfo(): MutableLiveData<TransferInfo> {
        return transferInfo
    }

    private val minerFee = MutableLiveData<String>()
    fun getMinerFee(): MutableLiveData<String> {
        return minerFee
    }

    val isSubmit = MutableLiveData(false)

    val address = MutableLiveData<String>()
    fun setAddressValue(value: String) {
        address.value = value
    }

    val amount = MutableLiveData<String>()
    fun setAmountValue(value: String) {
        amount.value = value
    }

    val remark = MutableLiveData<String>()

    var _address: String? = null
        set(value) {
            field = value
            inspectEditText()
        }
    var _amount: String? = null
        set(value) {
            field = value
            inspectEditText()
        }

    fun getGasInfo(coin: String? = "eth") {
        RetrofitUtils.apiService?.gasInfo(coin)
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe(object : BaseObserver<ResponseEntity<GasEntity>>() {
                override fun onSuccess(value: ResponseEntity<GasEntity>) {
                    if (value.code == 0) {
                        if (value.data?.steps?.size ?: 0 > 0) {
                            value.data?.defPosition = 2
                            value.data?.steps?.get(2)?.isSelected = true
                        } else {
                            value.data?.defPosition = 0
                            value.data?.steps?.get(0)?.isSelected = true
                        }
                        gasEntity.value = value.data
                        minerFee.value = Utils.calculate(value.data)
                    } else {
                        mErrorStr.postValue(value.msg)
                    }
                }

                override fun onFailed(e: Throwable) {

                }

            })
    }

    fun setStepSelect(position: Int) {
        val data = gasEntity.value
        data?.defPosition = position
        data?.steps?.forEach {
            it.isSelected = false
        }
        data?.steps?.get(position)?.isSelected = true
        gasEntity.value = data
        minerFee.value = Utils.calculate(data)
    }

    private fun inspectEditText() {
        var bol = false
        if (_address?.length ?: 0 > 0 && _amount?.length ?: 0 > 0) {
            try {
                if (_amount?.toFloat() ?: 0f > 0f) {
                    bol = true
                }
            } catch (e: Exception) {
                mErrorResId.postValue(R.string.inputAmountErrorTips)
            }
        }
        isSubmit.value = bol
    }

    fun getTransferInfo(coin: String?, token: String?, describe: String?, data: String?) {
        //封装转账信息
        val info = TransferInfo(
            coin,
            token,
            address.value,
            amount.value,
            gasEntity.value?.steps?.get(gasEntity.value?.defPosition ?: 0)?.gas,
            gasEntity.value?.gas,
            describe,
            gasEntity.value?.uint,
            gasEntity.value?.pow,
            data
        )
        transferInfo.value = info
    }

}